package com.zx.tradeclient.ctrl.index2;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.ActivityTaskManager;
import com.zx.base.util.URLHelper;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.ctrl.index5.LoginActivity;
import com.zx.tradeclient.entity.SupplyProductInfo;

public class SupplyInfoActivity extends BaseActivity implements OnClickListener {
	/** Called when the activity is first created. */

	private TextView supply_product_name;
	private TextView supply_info_unit_pirce;
	private TextView supply_info_introduce;
	private TextView supply_info_phone;
	private TextView supply_email;
	private TextView supply_info_company;
	private TextView supply_info_transaction;
	private EditText supply_to_buy_number;
	private ImageView supply_product_icon;

	private Button supply_info_order/* , product_info_record */;

	private String mProductID = "";
	private ProgressDialog progressDialog;

	private TradeApp app;
	private SupplyProductInfo info;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.supply_product_info);

		if (!(ActivityTaskManager.getInstance().containsName("SupplyInfoActivity"))) {
			ActivityTaskManager.getInstance().putActivity("SupplyInfoActivity", this);
		}

		init();

	}

	private void init() {

		app = (TradeApp) getMyApplication();

		setHeader(0);

		mProductID = getIntent().getStringExtra("productid");

		header_left.setOnClickListener(this);
		header_right.setText("收藏");
		header_right.setVisibility(View.VISIBLE);
		header_right.setOnClickListener(this);

		supply_product_name = (TextView) findViewById(R.id.supply_product_name);
		supply_info_unit_pirce = (TextView) findViewById(R.id.supply_info_unit_pirce);
		supply_info_introduce = (TextView) findViewById(R.id.supply_info_introduce);
		supply_info_phone = (TextView) findViewById(R.id.supply_info_phone);
		supply_email = (TextView) findViewById(R.id.supply_email);
		supply_info_company = (TextView) findViewById(R.id.supply_info_company);
		supply_info_transaction = (TextView) findViewById(R.id.supply_info_transaction);
		supply_to_buy_number = (EditText) findViewById(R.id.supply_to_buy_number);
		supply_product_icon = (ImageView) findViewById(R.id.supply_product_icon);

		/*
		 * product_info_record = (Button)
		 * findViewById(R.id.product_info_record);
		 * product_info_record.setOnClickListener(this);
		 */

		supply_info_order = (Button) findViewById(R.id.supply_info_order);
		supply_info_order.setOnClickListener(this);

		progressDialog = ProgressDialog.show(SupplyInfoActivity.this, null, "正在获取数据，请稍后.....", true, true);

		getSupplyProductInfo();
	}

	private void getSupplyProductInfo() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "supplyDetail");
		map.put("id", mProductID);

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseJson(response);
			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(SupplyInfoActivity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			}
		});
	}

	private void parseJson(JSONObject response) {
		try {
			JSONObject object = response;
			String succeed = object.getString("succeed");

			if (succeed.equals("000")) {

				info = new SupplyProductInfo();

				info.setProduct_id(object.getString("id"));

				app.imageLoader.DisplayImage(URLHelper.mPrefixImg + object.getString("photo"), supply_product_icon, R.drawable.page_icon_default);
				info.setProduct_icon(object.getString("photo"));

				supply_product_name.setText(object.getString("productName"));
				info.setProduct_name(object.getString("productName"));

				supply_info_unit_pirce.setText(object.getString("price"));
				info.setProduct_price(object.getString("price"));

				supply_info_introduce.setText(object.getString("remark"));
				info.setProduct_introduce(object.getString("remark"));

				supply_info_phone.setText(object.getString("linkMobile"));
				info.setProduct_phone(object.getString("linkMobile"));

				supply_email.setText(object.getString("email"));
				info.setProduct_email(object.getString("email"));

				info.setProduct_company_id(object.getString("userId"));

				supply_info_company.setText(object.getString("companyName"));
				info.setProduct_company(object.getString("companyName"));

				supply_info_transaction.setText(object.getString("tradeWay"));
				info.setProduct_payway(object.getString("tradeWay"));

			} else {
				Toast toast2 = Toast.makeText(SupplyInfoActivity.this, "无数据", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();
			}

			progressDialog.dismiss();

		} catch (JSONException e) {
			progressDialog.dismiss();
			e.printStackTrace();
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == header_left) {
			goback();
		} else if (v == header_right) {
			if (app.user.getId().equals("")) {
				Intent intent = new Intent(SupplyInfoActivity.this, LoginActivity.class);
				startActivity(intent);
			} else {
				progressDialog = ProgressDialog.show(SupplyInfoActivity.this, null, "正在提交数据，请稍后.....", true, true);
				productFavorite();
			}
		}/*
		 * else if(v == product_info_record){
		 * 
		 * }
		 */else if (v == supply_info_order) {

			 if(supply_to_buy_number.getText().toString().equals("")){
				 
				 Toast toast2 = Toast.makeText(SupplyInfoActivity.this, "请输入购买数量", Toast.LENGTH_SHORT);
					toast2.setGravity(Gravity.CENTER, 0, 0);
					toast2.show();
			 }else{
				 if (info != null) {
						if(app.user!=null && app.user.getId()!=null && app.user.getId()!=""){
							Intent intent = new Intent(SupplyInfoActivity.this, SupplyToBuyActivity.class);
							intent.putExtra("supplyId", info.getProduct_id());
							intent.putExtra("supplyNum", supply_to_buy_number.getText().toString());
							intent.putExtra("supplyName", info.getProduct_company());
							intent.putExtra("supplyPrice", info.getProduct_price());
							startActivity(intent);
						}else{
							Toast toast = Toast.makeText(SupplyInfoActivity.this, "您尚未登录", Toast.LENGTH_SHORT);
							toast.setGravity(Gravity.CENTER, 0, 0);
							toast.show();
						}
					}
			 }
			

		}

	}
	
	private void productFavorite() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "favourites");
		map.put("userId", app.user.getId());
		map.put("type", "3");
		map.put("infoId", mProductID);
		map.put("infoName", supply_product_name.getText().toString());

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				try {
					String succeed = response.getString("succeed");

					if (succeed.equals("000")) {

						Toast toast2 = Toast.makeText(SupplyInfoActivity.this, "收藏成功", Toast.LENGTH_SHORT);
						toast2.setGravity(Gravity.CENTER, 0, 0);
						toast2.show();

					} else {
						Toast toast2 = Toast.makeText(SupplyInfoActivity.this, "无数据", Toast.LENGTH_SHORT);
						toast2.setGravity(Gravity.CENTER, 0, 0);
						toast2.show();
					}

					progressDialog.dismiss();

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(SupplyInfoActivity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			}
		});
	}

}